package org.lc.service.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.lc.platform.base.dto.DeleteManyDto;
import org.lc.platform.base.result.Result;
import org.lc.platform.base.vo.PageVo;
import org.lc.platform.base.annotation.IsUUID;
import org.lc.platform.base.util.BeanValidatorUtil;
import org.lc.platform.mybatis.crud.controller.CrudBaseController;
import org.lc.platform.swagger.interfaces.Update;
import org.lc.service.system.dto.SysDictDetailDto;
import org.lc.service.system.dto.SysDictDto;
import org.lc.service.system.entity.SysDict;
import org.lc.service.system.entity.SysDictDetail;
import org.lc.service.system.service.SysDictService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dict")
@Validated
@Tag(name = "字典模块")
public class SysDictController extends CrudBaseController<SysDict, SysDict, SysDictDto, SysDictService> {
    public SysDictController(SysDictService service) {
        super(service);
    }

    @Operation(summary = "获取字典list")
    @GetMapping("/enum/{code}")
    private Result<List<SysDictDetail>> getDictEnumListByCode(@PathVariable("code") String code) {
        return Result.data(this.getService().getDictEnumListByCode(code));
    }

    @Operation(summary = "角色是否存在")
    @GetMapping("/exits/{code}")
    private Result<Boolean> getCodeExits(@PathVariable("code") String code) {
        return Result.data(this.getService().getCodeExits(code));
    }

    @Operation(summary = "刷新字典缓存")
    @GetMapping("/refresh/{code}")
    private Result<Boolean> refreshCache(@PathVariable("code") String code) {
        return Result.data(this.getService().handleCache(true, code));
    }

    @Operation(summary = "获取字典详情")
    @GetMapping("/detail/single")
    private Result<PageVo<SysDictDetail>> getDictDetail(@RequestParam("dict") String dict, SysDictDetailDto query) {
        BeanValidatorUtil.validate(query);
        return Result.data(this.getService().getDictDetail(dict, query));
    }

    @Operation(summary = "获取字典详情单条记录")
    @GetMapping("/detail/single/{detail}")
    private Result<SysDictDetail> getDictDetail(@PathVariable("detail") String detail) {
        return Result.data(this.getService().getDictDetailOne(detail));
    }

    @Operation(summary = "新增字典详情")
    @PostMapping("/detail/single")
    private Result<Boolean> createDictDetail(@RequestBody SysDictDetail body) {
        BeanValidatorUtil.validate(body);
        return Result.data(this.getService().createDictDetail(body));
    }

    @Operation(summary = "单条更新")
    @PatchMapping("/detail/single/{id}")
    public Result<Boolean> updateDictDetail(@IsUUID @PathVariable("id") String id, @Validated(value = {Update.class}) @RequestBody SysDictDetail data) {
        return Result.data(this.service.updateDictDetail(id, data));
    }

    @Operation(summary = "单条删除")
    @DeleteMapping("/detail/single/{id}")
    public Result<Boolean> deleteDictDetailOne(@IsUUID @PathVariable("id") String id) {
        return Result.data(this.service.deleteDictDetailOne(id));
    }

    @Operation(summary = "批量删除")
    @PostMapping("/detail/single/delete/many")
    public Result<Boolean> deleteDetailMany(@RequestBody DeleteManyDto data) {
        return Result.data(this.service.deleteDetailMany(data.getIds()));
    }

}